//
//  WeiBoDetailViewController.m
//  cityStar
//
//  Created by wangjia on 15/3/28.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "WeiBoDetailViewController.h"
#import "WeiBoCell.h"
#import "weiboCommentCell.h"
#import "UIScrollView+SVPullToRefresh.h"
#import "UIScrollView+SVInfiniteScrolling.h"

@interface WeiBoDetailViewController (){
    NSMutableArray *maryContent;
}
@property (nonatomic,assign) int page;
@end

@implementation WeiBoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addBackButton];
    self.navigationItem.title = @"微博详情";
    
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 0;
    
    __weak typeof(self)weakSelf = self;
    
    maryContent = [[NSMutableArray alloc]init];
    [self.tableView addPullToRefreshWithActionHandler:^{
        weakSelf.page = 1;
        [weakSelf webRequest_GetVideoMore:NO];
    }];
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf webRequest_GetVideoMore:YES];
    }];
    [self.tableView triggerPullToRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webRequest_GetVideoMore:(BOOL)isMore
{
    
    NSString *strUrl = [@"" stringByAppendingFormat:@"%@/page/%d/id/%@/type/%@",KinterFace_blogComment,isMore?self.page:0,self.weiboList.id_i,@"2"];
    
    [HttpRequestManager getMethod:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            if (isMore) {
                [maryContent addObjectsFromArray:[weiBoList getListArray:response[@"value"][@"content"]]];
                self.page = self.page + 1;
            }else{
                maryContent = [[NSMutableArray alloc]initWithArray:[weiBoList getListArray:response[@"value"][@"content"]]];
            }
            [self.tableView reloadData];
            //            if (maryContent.count == 0) {
            //                self.nothinglabel.hidden = NO;
            //            }else{
            //                self.nothinglabel.hidden = YES;
            //            }
        }
        if (isMore) {
            [self.tableView.infiniteScrollingView stopAnimating];
        }else{
            [self.tableView.pullToRefreshView stopAnimating];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 360;
    }
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return maryContent.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *cellIdentifier = @"CellWeiBo";
        WeiBoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        [cell setValueWithValue:self.weiboList];
        return cell;
    }
    if (indexPath.section == 1) {
        static NSString *cellIdentifier = @"CellWeboComment";
        weiboCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        weiBoList *weibo = maryContent[indexPath.row];
        cell.lblContent.text = weibo.comment;
        return cell;
    }
    return nil;
}
@end
